﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.Transefer
{
    public partial class FrmTranseferIn : Form
    {
        public FrmTranseferIn()
        {
            InitializeComponent();
        }

        DataTable DtIn;

        string realLocationSN;
        string InStorageSN;
        string InAreaSN;
        string partNumber;
        string dataCode;

        private void cmb_BillNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputBillNo();
        }

        private void cmb_BillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InputBillNo();
            }
        }

        private void txt_LocationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }
            if (string.IsNullOrEmpty(cmb_BillNo.Text))
            {
                MessageBox.Show("请先输入单号!");
                return;
            }

            realLocationSN = txt_LocationSN.Text;

            try
            {
                var snArray = realLocationSN.Split('#');

                var storageSN = snArray[0];
                var aresSN = snArray[0] + '#' + snArray[1];



                if (!string.IsNullOrEmpty(InStorageSN) && !InStorageSN.Equals(storageSN))
                {
                    MessageBox.Show("上架仓库" + InStorageSN + "与扫描的仓库" + storageSN + "不一致！");
                    return;
                }

                if (!string.IsNullOrEmpty(InAreaSN) && !InAreaSN.Equals(aresSN))
                {
                    MessageBox.Show("上架库区" + InAreaSN + "与扫描的库区" + aresSN + "不一致！");
                    return;
                }


                txt_LocationSN.Text = snArray[1] + snArray[2];
            }
            catch
            {
                MessageBox.Show("库位" + realLocationSN + "格式不正确，请重新输入!");
                txt_LocationSN.Text = "";
                return;
            }

            if (string.IsNullOrEmpty(txt_Qty.Text))
            {
                MessageBox.Show("移库单" + cmb_BillNo.Text + "临时库存不足，请选择其他移库单！");
                return;
            }

            var InveDT = Bll_Transefer.GetInventoryInfo(realLocationSN);

            if (InveDT.Rows.Count > 0)
            {
                if (!partNumber.Equals(InveDT.Rows[0]["PartNumber"].ToString()))
                {
                    MessageBox.Show("上架库位的物料号" + InveDT.Rows[0]["PartNumber"].ToString() + "与移库的物料号" + partNumber + "不一致!");
                    txt_LocationSN.Text = "";
                    return;
                }

                if (!dataCode.Equals(InveDT.Rows[0]["DateCode"].ToString()))
                {
                    MessageBox.Show("上架库位的DateCode" + InveDT.Rows[0]["DateCode"].ToString() + "与移库的DateCode" + dataCode + "不一致!");
                    txt_LocationSN.Text = "";
                    return;
                }
            }

            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

            DialogResult dr = MessageBox.Show("本次操作的上架库位：" + txt_LocationSN.Text + ", 请确认！", "上架确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

            if (dr == DialogResult.OK)
            {
                var qty = Convert.ToDecimal(txt_Qty.Text);

                var isSuccess = Bll_Transefer.TransferIn(cmb_BillNo.Text, realLocationSN, qty);

                if (isSuccess)
                {
                    lab_msg.Text = cmb_BillNo.Text + "上架成功!\r\n请选择其他单号！";

                    BindCMB();
                    cmb_BillNo.Focus();
                    txt_LocationSN.Text = "";
                    txt_Qty.Text = "";

                }
                else
                {
                    MessageBox.Show(cmb_BillNo.Text + "上架失败!" + txt_LocationSN.Text + "库位库存已被使用！");
                    txt_LocationSN.Text = "";
                }
            }
            else 
            {
                txt_LocationSN.Text = "";
                txt_LocationSN.Focus();
            }
        }


        public void BindCMB()
        {
            //status：1为下架成功的移库单，需要上架 
            DtIn = Bll_Transefer.GetTransferDT("1");

            cmb_BillNo.Text = "";
            cmb_BillNo.Items.Clear();

            if (DtIn.Rows.Count > 0)
            {
                foreach (DataRow dr in DtIn.Rows)
                {
                    cmb_BillNo.Items.Add(dr["BillNo"].ToString());
                }
            }
        }

        public void InputBillNo()
        {
            DataRow[] drIn = DtIn.Select(string.Format(" BillNo = '{0}'", cmb_BillNo.Text));
            if (drIn.Length > 0)
            {

                DataTable info = Bll_Transefer.GetTransferInfo(cmb_BillNo.Text);
                if (info.Rows.Count > 0)
                {
                    //if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                    //{

                    //txt_LocationSN.Text = info.Rows[0]["OutLocation_SN"].ToString();
                    //txt_Qty.Focus();

                    var inv = Bll_Transefer.GetInventoryInfo(cmb_BillNo.Text);
                    if (inv.Rows.Count > 0)
                    {

                        txt_Qty.Text = inv.Rows[0]["Qty"].ToString();

                        txt_LocationSN.Focus();

                        InStorageSN = info.Rows[0]["InStorage_SN"].ToString();
                        InAreaSN = info.Rows[0]["InArea_SN"].ToString();
                        partNumber = info.Rows[0]["PartNumber"].ToString();
                        dataCode = info.Rows[0]["DateCode"].ToString();
                        lab_msg.Text = "仓库:" + InStorageSN + "\r\n物料号:" + partNumber;
                    }
                    else
                    {
                        MessageBox.Show("移库单" + cmb_BillNo.Text + "临时库存不足，请选择其他移库单！");
                        cmb_BillNo.Text = "";
                        cmb_BillNo.Focus();
                    }
                    //}
                    //else
                    //{
                    //    MessageBox.Show(cmb_BillNo.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");

                    //    cmb_BillNo.Text = "";
                    //    txt_LocationSN.Text = "";
                    //    txt_Qty.Text = "";
                    //}
                }
            }
            else
            {
                //lab_msg.Text = cbx_Doc.Text + "单号不正确,请输入正确的单据!";
                MessageBox.Show(cmb_BillNo.Text + "单号不正确,请输入正确的单据!");
                cmb_BillNo.Text = "";
                cmb_BillNo.Focus();

            }
        }

        private void FrmTranseferIn_Load(object sender, EventArgs e)
        {
            BindCMB();
        }

    }
}